<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="账号" prop="accountId">
      <el-input v-model="dataForm.accountId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="角色" prop="role">
      <el-input v-model="dataForm.role" placeholder="0--管理员，1--发起者，2--投票者"></el-input>
    </el-form-item> -->
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="0--审核中，1--active，2--封禁中，-1--已注销"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <single-upload  v-model="dataForm.avatar"></single-upload>
      <!-- <el-input placeholder="头像地址"></el-input> -->
    </el-form-item>
    <el-form-item label="昵称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别，0--女，1--男，-1 -- 保密"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄 -1 -- 保密"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import SingleUpload from "@/components/upload/singleUpload.vue";
  export default {
    components: { SingleUpload },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          accountId: '',
          password: '',
          role: '',
          status: '',
          avatar: '',
          userName: '',
          gender: '',
          age: ''
        },
        dataRule: {
          accountId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          role: [
            { required: true, message: '0--管理员，1--发起者，2--投票者不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '0--审核中，1--active，2--封禁中，-1--已注销不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像地址不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别，0--女，1--男，-1 -- 保密不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄 -1 -- 保密不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/voteOnline/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.accountId = data.user.accountId
                this.dataForm.password = data.user.password
                this.dataForm.role = data.user.role
                this.dataForm.status = data.user.status
                this.dataForm.avatar = data.user.avatar
                this.dataForm.userName = data.user.userName
                this.dataForm.gender = data.user.gender
                this.dataForm.age = data.user.age
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/voteOnline/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'accountId': this.dataForm.accountId,
                'password': this.dataForm.password,
                'role': this.dataForm.role,
                'status': this.dataForm.status,
                'avatar': this.dataForm.avatar,
                'userName': this.dataForm.userName,
                'gender': this.dataForm.gender,
                'age': this.dataForm.age
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
